import { defineStore } from 'pinia'
import { ItabsState } from './types/tabs'

export const useTabsStore = defineStore('tabsView', {
  state: (): ItabsState => {
    return {
      tabsAction: '',
      tabsList: [
        {
          path: '/home',
          name: 'home',
          title: '首页',
          icon: 'HomeFilled',
          isShow: true,
        },
      ],
    }
  },
  //   异步，逻辑
  actions: {
    SET_TABSACTION(tabsAction: string) {
      this.tabsAction = tabsAction
    },
    SET_TABSLIST(tabsList: any) {
      this.tabsList = tabsList
    },
    REMOVE_TAB(handleName: string, currentName: string) {
      const itemIndex = this.tabsList.findIndex(
        (item) => item.name === handleName,
      )
      if (itemIndex !== -1) {
        this.tabsList.splice(itemIndex, 1)

        if (currentName === handleName) {
          // $router.push({ path: this.tabsList[itemIndex - 1].path })
          this.tabsAction = this.tabsList[itemIndex - 1].name
        }
      }
    },
  },
})
